package com.design.prototype;

/**
 * 原型模式
 *
 * @author <a href="jiangfeng.yan@ikang.com">jiangfeng</a>
 * @version 2021年06月17日 上午 9:49
 */
public class Person implements Cloneable {
    //姓名与年龄
    private String name;
    private int age;
    //添加一个引用类型的数据
    private Computer computer;

    public Computer getComputer() {
        return computer;
    }

    public void setComputer(Computer computer) {
        this.computer = computer;
    }

    public Person() {
    }
    public Person(String name, int age, Computer computer) {
        this.name = name;
        this.age = age;
        this.computer = computer;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", computer=" + computer +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person person = null;
        try {
            person = (Person) super.clone();
        }catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return person;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        Computer computer = new Computer("red","xiaomi");
        Person person = new Person("张三",20, computer);
        Person clonePerson= (Person)person.clone();
        System.out.println(person);
        System.out.println(clonePerson);

        //使用克隆对象修改 电脑属性值
        clonePerson.getComputer().setBrand("huawei");
        clonePerson.getComputer().setColor("black");

        System.out.println("克隆对象修改值后");
        System.out.println(person);
        System.out.println(clonePerson);
    }
}
